package com.ivvi.autoreco.model;

import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.io.File;

/**
 * Created by duanxianping on 2017/3/8.
 */

public class LocalImage extends BaseModel implements Comparable<LocalImage> {

    private String absPath;
    private String group;
    private long bucketId;//for group
    private long size;
    private long lastModified; //for order
    private int resId; // for camera

    @Override
    public int compareTo(@NonNull LocalImage o) {
        return (this._id == o.get_id() ? 0 : (this._id > o.get_id() ? -1 : 1));
    }

    public String getAbsPath() {
        return absPath;
    }

    public void setAbsPath(String absPath) {
        this.absPath = absPath;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public long getBucketId() {
        return bucketId;
    }

    public void setBucketId(long bucketId) {
        this.bucketId = bucketId;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }

    public int getImageWidth() {
        if (TextUtils.isEmpty(this.absPath)) {
            return 0;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(this.absPath, options);
        int imageWidth = options.outWidth;
        return imageWidth;
    }

    public int getImageHeight() {
        if (TextUtils.isEmpty(this.absPath)) {
            return 0;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(this.absPath, options);
        int imageHeight = options.outHeight;
        return imageHeight;
    }

    public Uri getUri() {
        if (TextUtils.isEmpty(this.absPath)) {
            return null;
        }
        File file = new File(this.absPath);
        return Uri.fromFile(file);
    }

    public String getSuffix() {
        if (TextUtils.isEmpty(this.absPath)) {
            return null;
        }
        String tmp = this.absPath.substring(this.absPath.lastIndexOf('.'));
        return tmp;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public File getFile() {
        if (TextUtils.isEmpty(this.absPath)) {
            return null;
        }
        return new File(this.absPath);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("");
        sb.append("_id:" + this.get_id() + ",");
        sb.append("abs:" + this.getAbsPath() + ",");
        sb.append("width:" + this.getImageWidth() + ",");
        sb.append("height:" + this.getImageHeight());

        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    //image type
    public enum ImageType {
        IMAGE_2D(0), IMAGE_3D(1), ACTION(3);
        private int val;

        ImageType(int value) {
            val = value;
        }
    }
}
